package com.cn.ycy.utils;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.util.Map;

public class SpringBeanUtils {

  /**
   * 通过springboot启动类，获取SpringBootApplication注解中scanBasePackages指定的扫描路径
   * @return 如果SpringBootApplication注解中scanBasePackages指定了扫描路径，返回一个数组，反之 null
   */
  public static String[] getBasePackagesFromSpringBootApplicationAnnotation(ApplicationContext applicationContext) {
    Map<String, Object> beanWithSprintBootApplicationAnnotation = applicationContext.getBeansWithAnnotation(SpringBootApplication.class);
    if(beanWithSprintBootApplicationAnnotation == null || beanWithSprintBootApplicationAnnotation.size() <=0 ) return null;
    for (Map.Entry<String, Object> stringObjectEntry : beanWithSprintBootApplicationAnnotation.entrySet()) {
      SpringBootApplication springBootApplication = stringObjectEntry.getValue().getClass().getAnnotation(SpringBootApplication.class);
      String[] scanBasePackages = springBootApplication.scanBasePackages();
      if (scanBasePackages.length == 0) continue;
      return scanBasePackages;
    }
    return null;
  }

}
